window._PLAYERSTATUS = {

    _CURRENTNODE:null,

    _NAME:{
        "allwomen":'女队通关',
        "108":'替天行道',
        "perfect":'完美通关',
        "normal":'普通通关',
        "killall":'单人通关',
        "sencondEnd":'二周目通关'
    },

    //108星
    liangshanMap:{},

    getTopMasterOfPlayer:function(){

        if(window.gameData.isMaster){
            return "无";
        }

        return window.gameData.master?window.gameData.master:'无'

    },


    getMasterBossOfPlayer:function(){

        var masterObj = window.gameData.country?_MASTER[window.gameData.country]:null;

        var masterName = '无';

        if(window.gameData.isMaster){
            masterName=window.gameData.playerTeam[0].name;
        }else{
            masterName = masterObj?masterObj.boss:'无';
        }

        return masterName;
    },

    showStatus:function(){

        var uiObj = _GRM.getNodeObj('status');
        var mainLabel = uiObj.node;
        _PLAYERSTATUS._CURRENTNODE = mainLabel;
        _PLAYERSTATUS.refreshStatusMsg('self');
        if(uiObj.isnew){
            mainLabel.getChildByName('mask').on(cc.Node.EventType.TOUCH_START, function(){
                _GRM.putNode(this);
            }, mainLabel);
            _PLAYERSTATUS.initLeftButton(mainLabel);

        }

        _MAP.node.addChild(mainLabel);
    },

    refreshStatusMsg:function(type){

        if(type=='self'){
            var msg = _PLAYERSTATUS.getPlayerStatus();
            _PLAYERSTATUS._CURRENTNODE.getChildByName('message').getComponent(cc.Label).string = msg;
        }else{
            var msg = _MASTERHELPER.getMasterIntro(type);
            _PLAYERSTATUS._CURRENTNODE.getChildByName('message').getComponent(cc.Label).string = msg;
        }

    },

    initLeftButton:function(mainLabel){

        var masterArr = ['self'];
        for(var key in _MASTER){
            masterArr.push(key);
        }

        var buttons = mainLabel.getChildByName('left')
                                .getChildByName('bigMap')
                                .getChildByName('view')
                                .getChildByName('content').getChildren();
        
        for(var i=0;i<buttons.length;i++){

            if(masterArr[i]){
                buttons[i].active = true;
                buttons[i].master = masterArr[i];

                buttons[i].on(cc.Node.EventType.TOUCH_START, function(){
                    //console.log(this.master);
                    _PLAYERSTATUS.refreshStatusMsg(this.master);
                }, buttons[i]);

                if(_MASTER[masterArr[i]]){
                    var masterConf = _MASTER[masterArr[i]];
                    buttons[i].getChildByName('Background')
                            .getChildByName('Label')
                            .getComponent(cc.Label).string = masterConf.name;
                    
                }else{
                    buttons[i].getChildByName('Background')
                            .getChildByName('Label')
                            .getComponent(cc.Label).string = '自身';
                }
            }else{
                buttons.active = false;
            }

        }

    },

    getPlayerStatusObj:function(){

        var masterObj = window.gameData.country?_MASTER[window.gameData.country]:null;
        var totalcitynmb = _ODATAHELPER.getCityNmb();
        var playerCountry = window.gameData.country;
        var masterName = _PLAYERSTATUS.getMasterBossOfPlayer();
        var topmasterName = _PLAYERSTATUS.getTopMasterOfPlayer();
        var titleName = '无';
        var countryName = (masterObj?masterObj.name:'无');
        var buyper = _GAMEDATAHELPER.getBuyPer();
        var sellper = _GAMEDATAHELPER.getSellPer();
        var citynmb = 0;
        var playernmb = 0;
        var totlenmb = 0;
        var killnmb = 0;
        var zmnmb = 0;
        var yhnmb = 0;
        var womentotal = 0;
        var womennmb = 0;
        var mannmb = 0;
        var liangshannmb = 0;

        if(playerCountry){
            var titleIndex = _CU.getLevelOfArr(_HONOUR._EXP,window.gameData.levelFight);
            var countryFlag = _MASTER[playerCountry].flag;
            titleName = countryFlag?_HONOUR._TITLE1[titleIndex]:_HONOUR._TITLE2[titleIndex];
        }

        for(var key in window.gameData.cityLog){
            if(window.gameData.cityLog[key]==window.gameData.country){
                citynmb++;
            }
        }

        for(var key in window.gameData.realPos){
            var npclog = window.gameData.realPos[key];
            if(_NPC[key]&&!_NPC[key].second){//二周目NPC不进入统计
                var iswomen = _NPC[key]&&(_NPC[key].sex==0);
                if(!npclog.die){
                    if(npclog.country==playerCountry&&!npclog.zm){
                        playernmb++;
                    }
                    if(npclog.zm){
                        zmnmb++;
                        if(iswomen)
                            womennmb++;
                        else
                            mannmb++;
                        if(_PLAYERSTATUS.liangshanMap[key]){
                            liangshannmb++;
                        }
                    }
                    if(npclog.f>=100){
                        yhnmb++;
                    }
                }else{
                    killnmb++;
                }

                if(iswomen){
                    womentotal++;
                }

                totlenmb++;
            }
        }

        return {

             masterObj:masterObj,
             totalcitynmb:totalcitynmb,
             citynmb:citynmb,
             playernmb:playernmb,
             totlenmb:totlenmb,
             killnmb:killnmb,
             zmnmb:zmnmb,
             playerCountry:playerCountry,
             masterName:masterName,
             topmasterName:topmasterName,
             titleName:titleName,
             countryName:countryName,
             buyper:buyper,
             sellper:sellper,
             yhnmb:yhnmb,
            womentotal:womentotal,
            womennmb:womennmb,
            mannmb:mannmb,
            liangshannmb:liangshannmb

        };

    },

    //获取等级上限
    getLevelMax:function(statusObj){

        if(_HDLVHELPER.getHdLv()!=0){
            if(!statusObj)
                statusObj = _PLAYERSTATUS.getPlayerStatusObj();

            var levelmax = 100;

            if(statusObj.citynmb>0){
                levelmax = 100+Math.floor(900*(statusObj.citynmb+5)/statusObj.totalcitynmb);
            }
             
            if(levelmax>1000){
                levelmax=1000;
            }
            return levelmax;
        }else{
            return 1000;
        }

    },

    getPlayerStatus:function(){

        var statusObj = _PLAYERSTATUS.getPlayerStatusObj();
        
        var result="势力:"+statusObj.countryName;
            result+="\n官职:"+statusObj.titleName;
            result+="\n首领:"+statusObj.masterName;
            result+="\n上级:"+statusObj.topmasterName;
            result+="\n城池:"+statusObj.citynmb+"/"+statusObj.totalcitynmb;
            result+="\n友军:"+statusObj.playernmb+"/"+statusObj.totlenmb;
            result+="\n招募:"+statusObj.zmnmb+"/"+statusObj.totlenmb;
            result+="\n击杀:"+statusObj.killnmb+"/"+statusObj.totlenmb;
            result+="\n买入:"+statusObj.buyper;
            result+="\n卖出:"+statusObj.sellper;
            result+="\n等级上限:"+_PLAYERSTATUS.getLevelMax(statusObj);
        
        
        return result;
    },

    //游戏结局
    //1.普通结局  占领所有城池
    //2.完美结局  占领所有城池  没有阵亡 且所有npc友好度100
    //3.单人通关  所有npc阵亡
    //4.女队通关  占领所有城池  招募所有女性npc 且手下中和友军中没有男性
    //5.替天行道  占领所有城池  势力为梁山泊 招募108将 没有其他手下和友军
    getGameEndType:function(){

        var statusObj = _PLAYERSTATUS.getPlayerStatusObj();

        if(statusObj.citynmb>=statusObj.totalcitynmb){
            if(
                statusObj.womennmb>=statusObj.womentotal
                &&statusObj.zmnmb<=statusObj.womennmb
                &&statusObj.playernmb==0
            ){
                return "allwomen";
            }else if(statusObj.zmnmb<=108
                    &&statusObj.liangshannmb==108
                    &&statusObj.playernmb==0
                ){
                return "108";
            }else if(statusObj.yhnmb==statusObj.totlenmb){
                return "perfect";
            }else{
                return "normal";
            }
        }else if(statusObj.killnmb==statusObj.totlenmb){
            return "killall";
        }

        return null;
    },

    getGameEndTypeStr:function(){

        var statusObj = _PLAYERSTATUS.getPlayerStatusObj();

        return [

            {
                name:'完美通关',
                content:'需要达成以下条件\n城池数:'+statusObj.citynmb+"/"+statusObj.totalcitynmb+"\n"
                        +'友好度100人数:'+statusObj.yhnmb+"/"+statusObj.totlenmb
            },

            {
                name:'普通通关',
                content:'需要达成以下条件\n城池数:'+statusObj.citynmb+"/"+statusObj.totalcitynmb
            },

            {
                name:'单人通关',
                content:'需要达成以下条件\n击杀数:'+statusObj.killnmb+"/"+statusObj.totlenmb
            },

            {
                name:'女队通关',
                content:'需要达成以下条件\n城池数:'+statusObj.citynmb+"/"+statusObj.totalcitynmb+"\n"
                        +'招募女性队员:'+statusObj.womennmb+"/"+statusObj.womentotal+"\n"
                        +'招募总人数:'+statusObj.zmnmb+"/"+statusObj.womentotal+"\n"
                        +'友军人数:'+statusObj.playernmb+"/0"
            },

            {
                name:'替天行道通关',
                content:'需要达成以下条件\n城池数:'+statusObj.citynmb+"/"+statusObj.totalcitynmb+"\n"
                        +'招募108星宿:'+statusObj.liangshannmb+"/108\n"
                        +'招募总人数:'+statusObj.zmnmb+"/108\n"
                        +'友军人数:'+statusObj.playernmb+"/0"
            }
        ]

    }


}